/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.ui;

import android.app.LocalActivityManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.util.ThemeManager;
import com.mol.market.common.util.TopBar;
import com.mol.market.common.util.Utils;
import com.mol.market.common.widget.BaseTabActivity;

/**
 * the view is displaying for rank tab in home
 * 
 * @author cong.li
 * @date 2011-5-9
 * @since Version 0.7.0
 */

public class RankTabActivity extends BaseTabActivity implements OnTabChangeListener {

    /** 排行榜100 */
    private static final int MAX_ITEMS = 100;
    private TabHost mTabHost;
    
    // 检查用户是否更换主题

    private BroadcastReceiver mThemeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            TopBar.initSkin(mSession, RankTabActivity.this);
            initSkin();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_tab);

        initTopBar();
        initView();
        
        IntentFilter themeFilter = new IntentFilter(Constants.BROADCAST_CHANGE_THEME);
        registerReceiver(mThemeReceiver, themeFilter);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mThemeReceiver);
        LocalActivityManager lam = getLocalActivityManager();
        lam.removeAllActivities();
    }
    
    private void initSkin() {

        View tabBg = findViewById(R.id.tab_frame_layout);
        tabBg.setBackgroundResource(ThemeManager.getResource(mSession, ThemeManager.KEY_TAB_BG));
        for (int i = 0; i < 3; i++) {
            TextView v = (TextView) mTabHost.getTabWidget().getChildTabViewAt(i);

            if (i == 0) {
                Utils.createTabView(getApplicationContext(), mSession,
                        getString(R.string.rank_tab_game), -1, v);
            } else if (i == 1) {
                Utils.createTabView(getApplicationContext(), mSession,
                        getString(R.string.rank_tab_app), 0, v);
            } else if (i == 2) {
                Utils.createTabView(getApplicationContext(), mSession,
                        getString(R.string.rank_tab_book), 1, v);
            }
        }
    }
    
    private void initView() {

        mTabHost = (TabHost) findViewById(android.R.id.tabhost);
        mTabHost.setup();
        mTabHost.getTabWidget().setPadding(mSession.mTabMargin9, 0, mSession.mTabMargin9, 0);
        FrameLayout fl = (FrameLayout) mTabHost.findViewById(R.id.tab_frame_layout);
        fl.setBackgroundResource(ThemeManager.getResource(mSession, ThemeManager.KEY_TAB_BG));

        Intent gameIntent = new Intent(getApplicationContext(), ProductListActivity.class);
        gameIntent.putExtra(Constants.EXTRA_CATEGORY, Constants.CATEGORY_GAME);
        gameIntent.putExtra(Constants.EXTRA_MAX_ITEMS, MAX_ITEMS);
        TabSpec tab2 = mTabHost
                .newTabSpec(Constants.CATEGORY_GAME)
                .setIndicator(
                        Utils.createTabView(getApplicationContext(), mSession, 
                                getString(R.string.rank_tab_game), -1, null)).setContent(gameIntent);
        mTabHost.addTab(tab2);
        
        Intent appIntent = new Intent(getApplicationContext(), ProductListActivity.class);
        appIntent.putExtra(Constants.EXTRA_CATEGORY, Constants.CATEGORY_APP);
        appIntent.putExtra(Constants.EXTRA_MAX_ITEMS, MAX_ITEMS);
        TabSpec tab1 = mTabHost
                .newTabSpec(Constants.CATEGORY_APP)
                .setIndicator(
                        Utils.createTabView(getApplicationContext(), mSession, 
                                getString(R.string.rank_tab_app), 0, null)).setContent(appIntent);
        mTabHost.addTab(tab1);

        Intent bookIntent = new Intent(getApplicationContext(), ProductListActivity.class);
        bookIntent.putExtra(Constants.EXTRA_CATEGORY, Constants.CATEGORY_EBOOK);
        bookIntent.putExtra(Constants.EXTRA_MAX_ITEMS, MAX_ITEMS);
        TabSpec tab3 = mTabHost
                .newTabSpec(Constants.CATEGORY_EBOOK)
                .setIndicator(
                        Utils.createTabView(getApplicationContext(), mSession, 
                                getString(R.string.rank_tab_book), 1,
                            null)).setContent(bookIntent);
        mTabHost.addTab(tab3);
        
//        // 2012/2/20 0.9.2添加收费排行榜
//        Intent growIntent = new Intent(getApplicationContext(), ProductListActivity.class);
//        growIntent.putExtra(Constants.EXTRA_CATEGORY, Constants.CATEGORY_PAID);
//        growIntent.putExtra(Constants.EXTRA_MAX_ITEMS, MAX_ITEMS);
//        TabSpec tab4 = mTabHost
//                .newTabSpec(Constants.CATEGORY_PAID)
//                .setIndicator(
//                        Utils.createTabView(getApplicationContext(), mSession,
//                                getString(R.string.rank_tab_paid), 1, null)).setContent(growIntent);
//        mTabHost.addTab(tab4);

        mTabHost.setOnTabChangedListener(this);
    }

    /**
     * 初始化Topbar
     */
    private void initTopBar() {
        TopBar.createTopBar(mSession, this, new View[] { findViewById(R.id.top_bar_title),
                findViewById(R.id.top_bar_search) }, new int[] { View.VISIBLE, View.VISIBLE },
                getString(R.string.rank_top_title));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return getParent().onKeyDown(keyCode, event);
    }

    @Override
    public void onTabChanged(String tabId) {
        if (Constants.CATEGORY_APP.equals(tabId)) {
            Utils.trackEvent(getApplicationContext(), Constants.GROUP_5, Constants.CLICK_RANK_APP);
        } else if (Constants.CATEGORY_GAME.equals(tabId)) {
            Utils.trackEvent(getApplicationContext(), Constants.GROUP_5, Constants.CLICK_RANK_GAME);
        } else if (Constants.CATEGORY_EBOOK.equals(tabId)) {
            Utils.trackEvent(getApplicationContext(), Constants.GROUP_5, Constants.CLICK_RANK_BOOK);
        } else if (Constants.CATEGORY_GROW.equals(tabId)) {
            Utils.trackEvent(getApplicationContext(), Constants.GROUP_5, Constants.CLICK_RANK_POP);
        }
    }
}
